package com.iretailer.report.analyze.flow.device.irisys;

import com.alibaba.fastjson.JSON;
import com.iretailer.report.analyze.annotation.AnalyzeClass;
import com.iretailer.report.analyze.commonModel.InOutMetric;
import com.iretailer.report.analyze.commonModel.PassengerFlowModel;
import com.iretailer.report.analyze.commonUtil.ResponeResultType;
import com.iretailer.report.analyze.flow.Flow;
import com.iretailer.report.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Auther: xjw
 * @Date: 2019/9/2 0002 10:39
 * @Description:
 */
@AnalyzeClass
@Component
public class Irisys extends Flow {
    private static Logger log = LoggerFactory.getLogger(Irisys.class);
    @Value("${netty.path.irisys}")
    private String irisys;

    private Map<String, CountLogs> lastDataMap = new HashMap<>();

    @Override

    public String[] analyzePath() {
        if (StringUtil.notEmpty(irisys)) {
            return irisys.split(",");
        } else {
            return new String[]{"/irisys"};
        }
    }


    @Override
    public PassengerFlowModel transformation(String context, Map<String, Object> paramsMap) {
        log.error("------ {}" , context);
        IrisysBean irisysBean = JSON.parseObject(context, IrisysBean.class);
        log.error("------ countLogs size {}", irisysBean.getCountLogs().size());
        PassengerFlowModel model = new PassengerFlowModel();
        model.setChannelCloseType(ResponeResultType.CFLType.CLOSE);
        model.setDeviceId(irisysBean.getFriendlyDeviceSerial());
        model.setIpAddress((String) paramsMap.get("remoreAddr"));
        model.setMacAddress(irisysBean.getMacAddress());
        model.setDeviceType("irisys4D");
        // 查询缓存中是否有上次的数据
        CountLogs lastData = lastDataMap.get(irisysBean.getFriendlyDeviceSerial());
        List<CountLogs> countLogs = irisysBean.getCountLogs();
        if (countLogs != null && countLogs.size() > 0) {
            // 如果只有一个历史数据
            if (lastData != null || countLogs.size() != 1) {
                List<InOutMetric> inOutMetricList = new ArrayList<>();
                for (CountLogs cl : countLogs) {
                    // 查询缓存中是否有上次的数据
                    CountLogs n_lastData = lastDataMap.get(irisysBean.getFriendlyDeviceSerial());
                    if (n_lastData != null) {
                        Map<Integer, Counts> lastDataMap = n_lastData.getCounts().stream().collect(Collectors.toMap(Counts::getRegisterId, a -> a));
                        List<Counts> counts = cl.getCounts();
                        if (counts != null && counts.size() > 0) {
                            for (Counts c : counts) {
                                Counts old = lastDataMap.get(c.getRegisterId());
                                if (old != null) {
                                    InOutMetric io = new InOutMetric();
                                    io.setZone(c.getName());
                                    int enter = (c.getValue() - old.getValue()) >= 0 ? c.getValue() - old.getValue() : c.getValue();
                                    io.setEnter(enter);
                                    ZonedDateTime oldDateTime = n_lastData.getTimestamp();
                                    // 这里只能用 ZoneDateTime
//                                    ZonedDateTime dateTime = cl.getTimestamp();
                                    io.setZonedDateTime(oldDateTime);
                                    inOutMetricList.add(io);
                                }
                            }
                        }
                    }
                    // 更新缓存中的数据
                    lastDataMap.put(irisysBean.getFriendlyDeviceSerial(), cl);
                }
                model.setInOutMetrics(inOutMetricList);
            } else {
                // 更新缓存中的数据
                lastDataMap.put(irisysBean.getFriendlyDeviceSerial(), countLogs.get(countLogs.size() - 1));
            }
        }
        return model;
    }


}
